package Homework.Top9;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

public class Market {
    public void show() {
        String path = "E:\\javaxiangmu\\Java-Collections\\src\\Day11\\Supermarket.xml";
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(new File(path));
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        List<Element> list = doc.selectNodes("op/product");
        for (Element e : list) {
            String name = e.selectSingleNode("name").getText();
            String itemno = e.selectSingleNode("itemno").getText();
            String price = e.selectSingleNode("price").getText();
            String stockcount = e.selectSingleNode("stockcount").getText();
            String unit = e.selectSingleNode("unit").getText();
            System.out.println("商品编号:" + itemno + " " + "商品名称:" + name + " " + "商品价格:" + price + " " + "商品库存:" + stockcount + unit);
        }
    }

    //查询指定的商品信息
    public void showProduct(String itemno1) {
        String path = "E:\\javaxiangmu\\Java-Collections\\src\\Day11\\Supermarket.xml";
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(new File(path));
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        List <Element> list = doc.selectNodes("shop/product[itemno=" + itemno1 + "]");
        if (list.size()==0){
            System.out.println("输入错误");
            return;
        }
        for (Element e : list) {
            String name = e.selectSingleNode("name").getText();
            String itemno = e.selectSingleNode("itemno").getText();
            String price = e.selectSingleNode("price").getText();
            String stockcount = e.selectSingleNode("stockcount").getText();
            String unit = e.selectSingleNode("unit").getText();
            System.out.println("商品编号:" + itemno + " " + "商品名称:" + name + " " + "商品价格:" + price + " " + "商品库存:" + stockcount + unit);
        }
    }

    //修改
    public boolean amendCommodity(String it, int st) {
        String path = "E:\\javaxiangmu\\Java-Collections\\src\\Day11\\Supermarket.xml";
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(new File(path));
        } catch (Exception e) {
            e.printStackTrace();
        }
        List <Element> list = doc.selectNodes("shop/product[itemno=' " + it +  "']");
        if (list.size()==0){
            System.out.println("输入错误");
        }
        for (Element e : list) {
            Element stockcounts = (Element) e.selectSingleNode("stockcount");
            stockcounts.setText(String.valueOf(st));
        }


        return false;
    }


    //购买商品
    public String buyCommodity(int itemno, int num) {
        String path = "E:\\javaxiangmu\\Java-Collections\\src\\Day11\\Supermarket.xml";
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(new File(path));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Element rootElem = doc.getRootElement();
        Attribute rootAttr = rootElem.attribute("discount");
        List <Element> list = doc.selectNodes("op/product/discount");
        for (Element e : list) {

        }
        return null;
    }
}
